package com.dingshang.dsapp.service;

import com.dingshang.dsapp.dto.NavigationDTO;
import com.dingshang.dsapp.entity.Navigation;
import com.dingshang.dsapp.mapper.NavigationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class NavigationService {

    @Autowired
    private NavigationMapper navigationMapper;

    public  List<NavigationDTO>  listAllNav() {
        return formatList(navigationMapper.listFirstKeyValue(-1), -1);
    }

    public List<NavigationDTO> listEnableKeyValue() {
        return formatList(navigationMapper.listFirstKeyValue(1), 1);
    }

    private List<NavigationDTO> formatList(List<Navigation> list, Integer status) {
        List<NavigationDTO> listDTO = new ArrayList<>(list.size());
        for (Navigation nav : list) {
            NavigationDTO dto = new NavigationDTO();
            listDTO.add(dto);

            dto.setId(nav.getId());
            dto.setNavHref(nav.getNavHref());
            dto.setNavTitle(nav.getNavTitle());
            dto.setParentId(nav.getParentId());
            dto.setStatus(nav.getStatus());
            dto.setNavTitleEn(nav.getNavTitleEn());

            List<Navigation> childList = navigationMapper.listSecondKeyValue(nav.getId(), status);
            List<NavigationDTO.NavigationInfo> childs = new ArrayList<>(childList.size());
            dto.setChilds(childs);

            for (Navigation navig : childList) {
                NavigationDTO.NavigationInfo info = new NavigationDTO.NavigationInfo();
                childs.add(info);
                info.setId(navig.getId());
                if ("装修案例".equals(nav.getNavTitle())) {
                    info.setNavHref("/" + navig.getId() + "/toAnli.html");
                } else {
                    info.setNavHref(navig.getNavHref());
                }

                info.setNavTitle(navig.getNavTitle());
                info.setParentId(navig.getParentId());
                info.setStatus(navig.getStatus());
                info.setNavTitleEn(navig.getNavTitleEn());
            }

//            if ("装修案例".equals(nav.getNavTitle())) {
//                dto.setNavHref(childs.get(0).getNavHref());
//            }

        }

        return listDTO;
    }

    public void saveAnliNav(String navName, String navNameEnglish) {
        Random random = new Random();

        Navigation navigation = new Navigation();
        navigation.setNavHref("/to" + random.nextInt(10000) + ".html");
        navigation.setNavTitle(navName);
        navigation.setNavTitleEn(navNameEnglish);
        navigation.setParentId(2);
        navigation.setStatus(0);

        navigationMapper.insert(navigation);
    }

    public List<Navigation> listAnliSecond() {
        return navigationMapper.listSecondKeyValue(2, -1);
    }

    public List<Navigation> listEnableAnliSecond() {
        return navigationMapper.listSecondKeyValue(2, 1);
    }

    public void changeAnliStatus(int id, int status) {
        Navigation navigation = navigationMapper.selectByPrimaryKey(id);
        navigation.setStatus(status);
        navigationMapper.updateByPrimaryKey(navigation);
    }
}
